<?php

/**
 * message actions.
 *
 * @package    maximous
 * @subpackage message
 * @author     Mina.Gerges.Wadea
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class messageActions extends MaxControllerActions
{

    public function preExecute()
    {
        parent::preExecute();
        $this->maxUser->getMaxUserRole()->setDefaultSiteComponent(SiteComponent::Message);
    }

    public function executeIndex(sfWebRequest $request)
    {
        //check access
        $this->checkAccess($this->maxUser->getMaxUserRole()->canView());
        $this->formFilter = new MessageFormFilter(array(), array('maxUser' => $this->maxUser));
//        echo $this->formFilter->getWidgetSchema()->getNameFormat();
        $this->formFilter->bind($request->getParameter($this->formFilter->getName()));
        $query    = $this->formFilter->buildQuery($this->formFilter->getValues());
        $page     = $request->getParameter('page', 1);
        $this->pager = $this->createPager('Message', $query, $page, sfConfig::get('app_table_list_row_numbers'));
    }

    public function executeNew()
    {
        //check access
        $this->checkAccess($this->maxUser->getMaxUserRole()->canAdd());
        $this->form = new MessageForm(null, array('i18n'    => $this->i18n, 'maxUser' => $this->maxUser));
    }

    public function executeCreate(sfWebRequest $request)
    {
        //check access
        $this->checkAccess($this->maxUser->getMaxUserRole()->canAdd());
        $this->forward404Unless($request->isMethod(sfRequest::POST));

        $this->form = new MessageForm(null, array('i18n'    => $this->i18n, 'maxUser' => $this->maxUser));
        $this->form->bind($request->getParameter($this->form->getName()), $request->getFiles($this->form->getName()));

        if ($this->form->isValid())
        {
            $this->form->save();
            if ($this->form->getObject()->sendMe())
                ;

            $this->getUser()->setFlash('message_success', $this->i18n->__('send message successful'));
            $this->redirect('message/index');
        }
        else
        {
            $this->getUser()->setFlash('message_failed', $this->i18n->__('send message failed'));
        }

        $this->setTemplate('new');
    }

    public function executeDelete(sfWebRequest $request)
    {
        //check access
        $this->checkAccess($this->maxUser->getMaxUserRole()->canDelete());

        $this->forward404Unless($message = Doctrine_Core::getTable('Message')->find(array($request->getParameter('id'))), sprintf('Object message does not exist (%s).', $request->getParameter('id')));
        $this->getUser()->setFlash('message_success', $this->i18n->__('send message successful'));
        $message->delete();
        $this->redirect('message/index');
    }

    public function executeSendMessage(sfWebRequest $request)
    {
        $this->forward404Unless($this->maxUser->getMaxUserRole()->canAdd());
        $messageForm = new MessageForm(null, array('i18n'    => $this->i18n, 'maxUser' => $this->maxUser));
        $valid    = '';
        $html     = '';
        if ($request->isMethod(sfWebRequest::POST))
        {
            $messageForm->bind($request->getParameter($messageForm->getName()));
            if ($messageForm->isValid())
            {
                $messageForm->save();
                $valid = true;
            }
        }
        if (!$valid)
        {
            $html = $this->getPartial('sendMessageForm', array('messageForm' => $messageForm));
        }
        return $this->renderJson(array('html'  => $html, 'valid' => $valid));
    }

}
